//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!
/***************************/

//alert(handleResize());
//document.getElementById("accordion").style.height=

var Flag_Search_Panel_Status="BIO";
var selectedbutton=true;


$(document).ready(function(){
   $(document).mousemove(function(e){
       if (e.pageX>=(document.documentElement.clientWidth-30) && e.pageX<=(document.documentElement.clientWidth-10) && e.pageY>=35 && e.pageY<=55 && $(".trigger").is(":visible")==false)
       {
           document.getElementById("searchbox").style.cursor='pointer';
           
       }
       else if (e.pageX>=(document.documentElement.clientWidth-50) && e.pageX<=(document.documentElement.clientWidth-30) && e.pageY>=35 && e.pageY<=55 && $(".trigger").is(":visible")==true)
       {
           document.getElementById("searchbox").style.cursor='pointer';

       }
       else
       {
           document.getElementById("searchbox").style.cursor='text';
           
       }
       
   });

   $(document).click(function(e){
       if (e.pageX>=(document.documentElement.clientWidth-30) && e.pageX<=(document.documentElement.clientWidth-10) && e.pageY>=35 && e.pageY<=55 && $(".trigger").is(":visible")==false)
       {
           SearchProcess();

       }
       else if (e.pageX>=(document.documentElement.clientWidth-50) && e.pageX<=(document.documentElement.clientWidth-30) && e.pageY>=35 && e.pageY<=55 && $(".trigger").is(":visible")==true)
       {
           SearchProcess();

       }


   });
});


function SearchProcess()
{
    if($("#searchbox").val()!="")
                                    {
                $(".trigger").show();
                if($(".panel").is(":visible")==false){


                $(".panel").toggle();

                $(".trigger").toggleClass("active");
		}
                var cValue = $("#searchbox").val();
                $("#searchdetailgrid").html("No data found");
                    $("#tdsearchresult").html('<img src="/images/loading/loading9.gif">');

                    delGraphMarker();
                    if($(".panel_detail").is(":visible")==true){
                   graphShowButton(false);
                showhidedetails();
                }

                    
                    searchaction();

                $.post("http://"+location.hostname+"/map/Search", {search: cValue.replace("'","''") +"flag_search_d_pg_t"+"ALL"}, function(data) {
                                $("#tdsearchresult").html(data);

                                setAccordion();



                                //document.getElementById("map_canvas").style.width  ="930px";

                              if(document.getElementById("accordion")){document.getElementById("accordion").style.height  = (document.documentElement.clientHeight-60) + "px";}
                              if(document.getElementById("divbiodata")){document.getElementById("divbiodata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                              if(document.getElementById("divnonbiodata")){document.getElementById("divnonbiodata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                              if(document.getElementById("divoverlaydata")){document.getElementById("divoverlaydata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                });

                                    }
}

$(document).ready(function() {

	//Default Action
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
        

	//On Click Event
	$("ul.tabs li").click(function() {
            
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content

                
                if($(".panel_detail").is(":visible")==false){
                
                showhidedetails();
                }
                else if ($(".panel_detail").is(":visible")==true && selectedbutton==$(this).find("a").attr("id"))
                    {
                        if($(".panel_detail").is(":visible")==true){
                                
                                showhidedetails();
                }
                    }
                    
                    if (selectedbutton!=$(this).find("a").attr("id") && $(".panel_detail").is(":visible")==true && $(this).find("a").attr("id")=="btngraph")
                        
                        {
                          loadgraph("");
                        }

                    
                    selectedbutton=$(this).find("a").attr("id");

		return false;
	});

        




});







$(document).ready(function() {
		
                $(".panel").toggle();
                $(".panel_detail").toggle();
                
                document.getElementById("hdSearchDetails").value="0";
                $('a#markerbutton').toggleClass("down");

		


	});

    

$(document).ready(function() {

document.getElementById("searchresult").style.height  = (document.documentElement.clientHeight-85) + "px";



});







$(document).ready(function() {

    $("#searchbox").keypress(function(event) {
        if (event.which == '13') {
            SearchProcess();
        }
                                });


});




function setAccordion()
{
if($('#ac1').is(":visible")==true)
{
    Flag_Search_Panel_Status="BIO";
}
else if($('#ac2').is(":visible")==true && $('#ac1').is(":visible")==false)
{
    Flag_Search_Panel_Status="NONBIO";
}
else if($('#ac3').is(":visible")==true && $('#ac1').is(":visible")==false && $('#ac2').is(":visible")==false)
{
    Flag_Search_Panel_Status="OVERLAY";
}

$("#accordion").accordion({collapsible: true,

                    change: function(event, ui) {

                     if (ui.newHeader.text().indexOf("Species Data:")==0)
                         {
                             Flag_Search_Panel_Status="BIO";
                             searchaction("chkbio");
                             $("#divUnitFilter").hide();
                         }
                    else if (ui.newHeader.text().indexOf("Specific Units:")==0)
                         {
                             Flag_Search_Panel_Status="NONBIO";
                             searchaction("chknonbio");
                             $("#divUnitFilter").show();
                         }
                    else if (ui.newHeader.text().indexOf("Overlay Data")==0)
                         {
                             Flag_Search_Panel_Status="OVERLAY";
                             searchaction("chkoverlay");
                             
                         }
                             
                      }
}
            );
}
$(document).ready(function() {


});


$(window).resize(function() {
document.getElementById("searchresult").style.height  = (document.documentElement.clientHeight-85) + "px";
document.getElementById("GraphBio_Bar").style.height  = (document.documentElement.clientHeight*20)/100 + "px";

document.getElementById("searchresultdetailtab").style.height  = (document.documentElement.clientHeight-130) + "px";
                              if(document.getElementById("accordion")){document.getElementById("accordion").style.height  = (document.documentElement.clientHeight-60) + "px";}
                              if(document.getElementById("divbiodata")){document.getElementById("divbiodata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                              if(document.getElementById("divnonbiodata")){document.getElementById("divnonbiodata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                              if(document.getElementById("divoverlaydata")){document.getElementById("divoverlaydata").style.height  = (document.documentElement.clientHeight-175) + "px";}

loadgraph("");
        });





$(document).ready(function(){
	$(".trigger").click(function(){
            $(this).toggleClass("active");
            if (document.getElementById("hdtrigger").value=="0")
                {document.getElementById("hdtrigger").value=1;
                        document.getElementById("GraphBio_Bar").style.width  = (document.documentElement.clientWidth-357) + "px";
                        document.getElementById("searchresultdetailtab").style.width  = (document.documentElement.clientWidth-357) + "px";
                        document.getElementById("searchresultcitationtab").style.width  = (document.documentElement.clientWidth-357) + "px";
                        document.getElementById("divfilter").style.width  = (document.documentElement.clientWidth-395) + "px";
                        document.getElementById("imgtrigger").src='/images/up.png';
                        loadgraph("");
                    }
            else if (document.getElementById("hdtrigger").value=="1")
                {document.getElementById("hdtrigger").value=0;
                    document.getElementById("GraphBio_Bar").style.width  = (document.documentElement.clientWidth-30) + "px";
                        document.getElementById("searchresultdetailtab").style.width  = (document.documentElement.clientWidth-30) + "px";
                        document.getElementById("searchresultcitationtab").style.width  = (document.documentElement.clientWidth-30) + "px";
                        document.getElementById("divfilter").style.width  = (document.documentElement.clientWidth-68) + "px";
                        document.getElementById("imgtrigger").src='/images/down.png';
            loadgraph("");
            }

                $(".panel").toggle();
		


               


		return false;
	});
});



function loadpage(a,tg)
{
    if($("#searchbox").val()!="")
           {
                var cValue = $("#searchbox").val();

    $.post("http://"+location.hostname+"/map/Search", {search: cValue+"flag_search_d_pg_t"+tg+"flag_search_d_pg_t"+a}, function(data) {
                                
                                
                                if(tg=="BIO")
                                    {
                                        
                                        $("#divbiodata").html(data);
                                        
                                    }
                                else if(tg=="NONBIO")
                                    {
                                        
                                        $("#divnonbiodata").html(data);
                                    }
                                else if(tg=="OVERLAY")
                                    {
                                        $("#divoverlaydata").html(data);
                                    }
                              if(document.getElementById("divbiodata")){document.getElementById("divbiodata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                              if(document.getElementById("divnonbiodata")){document.getElementById("divnonbiodata").style.height  = (document.documentElement.clientHeight-175) + "px";}
                              if(document.getElementById("divoverlaydata")){document.getElementById("divoverlaydata").style.height  = (document.documentElement.clientHeight-175) + "px";}
    });
    }
}


function selectall(oid)
{
    var sall=true;
    var objname="";
    if (oid=="selectallbio")
        {sall=true;objname="chkbio";}
    else if (oid=="selectallnonbio")
        {sall=true;objname="chknonbio";}
    else if (oid=="selectalloverlay")
        {sall=true;objname="chkoverlay";}
    else if (oid=="unselectallbio")
        {sall=false;objname="chkbio";}
    else if (oid=="unselectallnonbio")
        {sall=false;objname="chknonbio";}
    else if (oid=="unselectalloverlay")
        {sall=false;objname="chkoverlay";}

    var v=document.getElementsByName(objname);
    for (i=0;i<v.length;i++)
        {
            v[i].checked=sall;
        }


    searchaction(objname);

}



function getBioselection()
{
    var v=document.getElementsByName("chkbio");
    var str="";
    
    for (i=0;i<v.length;i++)
        {
            if (v[i].checked)
                {
                    if (str!="")
                    {
                        str=str+","
                    }
                    str=str+v[i].id;
                    
                }
        }

        return str;
}
function getNonbioselection()
{
    var v=document.getElementsByName("chknonbio");
    var str="";

    for (i=0;i<v.length;i++)
        {
            if (v[i].checked)
                {
                    if (str!="")
                    {
                        str=str+","
                    }
                    str=str+v[i].id;

                }
        }

        return str;
}
function getOverlayselection()
{
    var v=document.getElementsByName("chkoverlay");
    var str="";

    for (i=0;i<v.length;i++)
        {
            if (v[i].checked)
                {
                    if (str!="")
                    {
                        str=str+","
                    }
                    str=str+v[i].id;

                }
        }

        return str;
}

function searchaction(tg)
{
    delGraphMarker();
    if (tg=="chkbio")
        {
        var bioselection=getBioselection();

        loadMarker(map,bioselection+"flg_bio_and_nonbio_map_point");
        
        setOverlayImage(map,"");

        if (bioselection!="")
            {
                graphShowButton(true);
                if($("#panel_detail").is(":visible")==true){

                    flag_search_loadsearchlocations(bioselection,"BIO");
                    flag_search_loadsearchdepths(bioselection,"BIO");
                    
                    flag_search_loadsearchyears(bioselection,"BIO");
                   
                       loadgraph(bioselection);
                       
                    //SetBioChart(bioselection,null,"o.location");

                    loadDetailgrid(bioselection,"BIO");
                    loadCitation(bioselection,"BIO");
                    
                }
            }
       else
           {
               graphShowButton(false);
               $("#panel_detail").hide();
               return;
           }
        
        
        
        }

        else if ( tg=="chknonbio")
        {
        var nonbioselection=getNonbioselection();
        loadMarker(map,"flg_bio_and_nonbio_map_point"+nonbioselection);

        setOverlayImage(map,"");
        
        if (nonbioselection!="")
            {
            graphShowButton(true);
                if($("#panel_detail").is(":visible")==true){
            flag_search_loadsearchlocations(nonbioselection,"NONBIO");
            flag_search_loadsearchdepths(nonbioselection,"NONBIO");
            flag_search_loadsearchyears(nonbioselection,"NONBIO");
            flag_search_loadsearchunits(nonbioselection,"NONBIO");
            //SetNonBioChart(nonbioselection,null,"o.location");
            loadgraph(nonbioselection);
            
            loadDetailgrid(nonbioselection,"NONBIO");
            loadCitation(nonbioselection,"NONBIO");
                }
        }
         else
           {
               graphShowButton(false);
               $("#panel_detail").hide();
               return;
           }
        }
        

    

else if (tg=="chkoverlay" )
    {
        var overlayselection=getOverlayselection();
//        setOverlayImage(map,overlayselection);
        setOverlayImageWithOpacity(map,overlayselection, 90);
        loadDetailgrid(overlayselection,"OVERLAY");
        loadCitation(overlayselection,"OVERLAY");
        loadMarker(map,"");
        $('#GraphBio_Bar').html("");
    }
else
{
    loadMarker(map,"");
}
    
}

$(document).ready(function(){
    $('a#markerbutton').click(function(){
        $(this).toggleClass("down");
        if (document.getElementById("hdShowMarkers").value=="1")
            {
                document.getElementById("hdShowMarkers").value="0";

            }
        else if (document.getElementById("hdShowMarkers").value=="0")
            {
                document.getElementById("hdShowMarkers").value="1";

            }
                loadMarker(map,getBioselection()+"flg_bio_and_nonbio_map_point"+getNonbioselection());
            
    });


});



     $(function() {


                $.post("http://"+location.hostname+"/map/Autocomplete", {}, function(data) {
                $( "#searchbox" ).autocomplete(data.split("###"),{
                minChars: 1,
		max: 10,
		autoFill: false,
		mustMatch: false,
		matchContains: "word",
		scrollHeight: 220

//                        minLength: 2,
//                        select: function( event, ui ) {
//

//                        }
		}
		);

                    $("#searchbox").result(function() {


                      SearchProcess();

    });


                });
	});
function loadCitation(para,tg)
{

    $("#searchresultcitationtab").html('<div style="height: 100%; width: 100%; background: url(/images/loading/loading9.gif) 45% 50% no-repeat">  </div>');
                    $.post("http://"+location.hostname+"/map/searchcitation", {search: para+"flag_search_citation"+tg}, function(data) {
                              $("#searchresultcitationtab").html(data);
                });
    


}
$(document).ready(function(){
	$(".grptype").click(function(){

                if (document.getElementById("hdgrptype").value=="0")
                    {
                        document.getElementById("hdgrptype").value="1";
                        $(this).html("Show by Location");
                        $("#divLocationFilter").show();
                        $("#divYearFilter").hide();
                        $(this).toggleClass("active");
                        //$("#lbgraphtitle").html("Yearly Summary");
                     loadgraph("");
                    }
                else if (document.getElementById("hdgrptype").value=="1")
                    {
                        document.getElementById("hdgrptype").value="0";
                        $(this).html("Show by year");
                        $("#divLocationFilter").hide();
                        $("#divYearFilter").show();
                        $(this).toggleClass("active");
                        //$("#lbgraphtitle").html("Location Summary");
                     loadgraph("");
                    }


		
	});
});
function flag_search_loadsearchlocations(para,tg)
{
    $.post("http://"+location.hostname+"/map/Loadsearchresultlocations", {search: para+"flag_search_loadsearchresultlocations"+tg}, function(data) {
        $('#cbLocationFilter').html(data);
});
}
function flag_search_loadsearchdepths(para,tg)
{
    $.post("http://"+location.hostname+"/map/Loadsearchresultdepths", {search: para+"flag_search_loadsearchresultdepths"+tg}, function(data) {
        $('#cbDepthFilter').html(data);
});
}
function flag_search_loadsearchunits(para,tg)
{
    $.post("http://"+location.hostname+"/map/Loadsearchresultunits", {search: para+"flag_search_loadsearchresultunits"+tg}, function(data) {
        $('#cbUnitFilter').html(data);
});
}
function flag_search_loadsearchyears(para,tg)
{
    $.post("http://"+location.hostname+"/map/Loadsearchresultyears", {search: para+"flag_search_loadsearchresultyears"+tg}, function(data) {
        $('#cbYearFilter').html(data);
});

}

$(document).ready(function(){
	$("#cbYearFilter").change(function(){
        
        

        loadgraph("");
        
        });

        });
$(document).ready(function(){
	$("#cbDepthFilter").change(function(){



        loadgraph("");

        });

        });
$(document).ready(function(){
	$("#cbUnitFilter").change(function(){



        loadgraph("");

        });

        });

$(document).ready(function(){
	$("#cbLocationFilter").change(function(){

        loadgraph("");
        });

        });


function graphShowButton(flg)
{
    if(flg==true)
        {
        $("#toolsearchresult").show();}
    else
        {
    $("#toolsearchresult").hide();}
}

    



function showhidedetails()
{
    if (document.getElementById("hdSearchDetails").value=="0")
                    {
                        document.getElementById("hdSearchDetails").value="1";

                        $("#panel_detail").show();

                        if($("#panel").is(":visible")==false){
                            document.getElementById("GraphBio_Bar").style.width  = (document.documentElement.clientWidth-357) + "px";
                            document.getElementById("searchresultdetailtab").style.width  = (document.documentElement.clientWidth-357) + "px";
                            document.getElementById("searchresultcitationtab").style.width  = (document.documentElement.clientWidth-357) + "px";
                            
                            document.getElementById("divfilter").style.width  = (document.documentElement.clientWidth-395) + "px";

                        }
                        else
                        {
                            document.getElementById("GraphBio_Bar").style.width  = (document.documentElement.clientWidth-30) + "px";
                            document.getElementById("searchresultdetailtab").style.width  = (document.documentElement.clientWidth-30) + "px";
                            document.getElementById("searchresultcitationtab").style.width  = (document.documentElement.clientWidth-30) + "px";
                            document.getElementById("divfilter").style.width  = (document.documentElement.clientWidth-68) + "px";
                        }

                        document.getElementById("GraphBio_Bar").style.height  = (document.documentElement.clientHeight*20)/100 + "px";
                        document.getElementById("searchresultdetailtab").style.height  = (document.documentElement.clientHeight-130) + "px";

                        if (Flag_Search_Panel_Status=="BIO")
                             searchaction("chkbio");
                        if (Flag_Search_Panel_Status=="NONBIO")
                             searchaction("chknonbio");



                    }
                else if (document.getElementById("hdSearchDetails").value=="1")
                    {
                        document.getElementById("hdSearchDetails").value="0";
                        $("#panel_detail").hide();


                    }
}
function loadgraph(selval)
{
    var tmp=null;
    var tmpdepth=null;
    var tmpunit=null;

    tmpdepth=$("#cbDepthFilter").val();
    tmpunit=$("#cbUnitFilter").val();









    if (document.getElementById("hdgrptype").value=="1")
                    {


                         tmp=$("#cbLocationFilter").val();
                        if (tmp=="All" || tmp==null)
                           {
                              if (Flag_Search_Panel_Status=="BIO")
                                {if(selval==""){selval=getBioselection();}SetBioChart(selval,tmp,"o.observationdate",tmpdepth);}
                             else if (Flag_Search_Panel_Status=="NONBIO")
                                 {if(selval==""){selval=getNonbioselection();}SetNonBioChart(selval,tmp,"o.observationdate",tmpdepth,tmpunit);}
                           }
                        else{
                               if (Flag_Search_Panel_Status=="BIO")
                                {if(selval==""){selval=getBioselection();}SetBioChart(selval,tmp,"year",tmpdepth);}
                               else if (Flag_Search_Panel_Status=="NONBIO")
                                   {if(selval==""){selval=getNonbioselection();}SetNonBioChart(selval,tmp,"year",tmpdepth,tmpunit);}
                             }

                    }
                else if (document.getElementById("hdgrptype").value=="0")
                    {

                        tmp=$("#cbYearFilter").val();
                      
                        if (tmp=="All" || tmp==null)
                           {
                              if (Flag_Search_Panel_Status=="BIO")
                                {if(selval==""){selval=getBioselection();}SetBioChart(selval,tmp,"o.location",tmpdepth);}
                             else if (Flag_Search_Panel_Status=="NONBIO")
                                 {if(selval==""){selval=getNonbioselection();}SetNonBioChart(selval,tmp,"od.observationdepth",tmpdepth,tmpunit);}
                           }
                        else{
                               if (Flag_Search_Panel_Status=="BIO")
                                {if(selval==""){selval=getBioselection();}SetBioChart(selval,tmp,"location",tmpdepth);}
                               else if (Flag_Search_Panel_Status=="NONBIO")
                                   {if(selval==""){selval=getNonbioselection();}SetNonBioChart(selval,tmp,"observationdepth",tmpdepth,tmpunit);}
                             }



                    }
}